﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Globalization

Namespace System.Workflow.ComponentModel
    Friend NotInheritable Class TimeoutDurationConverter
        Inherits TypeConverter
        ' Methods
        Public Overrides Function CanConvertFrom(ByVal context As ITypeDescriptorContext, ByVal sourceType As Type) As Boolean
            Return ((sourceType Is GetType(String)) OrElse MyBase.CanConvertFrom(context, sourceType))
        End Function

        Public Overrides Function CanConvertTo(ByVal context As ITypeDescriptorContext, ByVal destinationType As Type) As Boolean
            Return ((destinationType Is GetType(String)) OrElse MyBase.CanConvertTo(context, destinationType))
        End Function

        Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object) As Object
            Dim zero As TimeSpan = TimeSpan.Zero
            Dim str As String = [TryCast](value,String)
            If Not String.IsNullOrEmpty(str) Then
                Try 
                    zero = TimeSpan.Parse(str)
                Catch obj1 As Object
                End Try
                If (zero.Ticks < 0) Then
                    Throw New Exception(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_NegativeValue"), New Object() { value.ToString, "TimeoutDuration" }))
                End If
            End If
            Return zero
        End Function

        Public Overrides Function ConvertTo(ByVal context As ITypeDescriptorContext, ByVal culture As CultureInfo, ByVal value As Object, ByVal destinationType As Type) As Object
            If ((destinationType Is GetType(String)) AndAlso TypeOf value Is TimeSpan) Then
                Dim span As TimeSpan = DirectCast(value, TimeSpan)
                Return span.ToString
            End If
            Return MyBase.ConvertTo(context, culture, value, destinationType)
        End Function

        Public Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As StandardValuesCollection
            Dim values As New ArrayList
            values.Add(New TimeSpan(0, 0, 0))
            values.Add(New TimeSpan(0, 0, 15))
            values.Add(New TimeSpan(0, 1, 0))
            Return New StandardValuesCollection(values)
        End Function

        Public Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
            Return True
        End Function

    End Class
End Namespace

